package com.quanyan.user.thread;

import com.quanyan.club.mq.ClubMedalMsg;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.constants.MqConstants;
import com.quanyan.common.enums.SysClubMedalConditionEnum;
import com.quanyan.common.utils.*;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.user.constants.UserConstants;
import com.quanyan.user.model.db.TbUser;
import com.quanyan.user.model.db.TbUserExt;
import com.quanyan.user.model.vo.request.UserInfoPerfectStatus;
import com.quanyan.user.mq.Producer.UserTaskMsgClientImpl;
import com.quanyan.user.mq.UserTaskMsg;
import com.quanyan.user.service.UserExtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Created by zhang on 16/8/6.
 */
public class UserInformationPerfectDegreeThread implements Runnable{

    private final static Logger logger = LoggerFactory.getLogger(UserInformationPerfectDegreeThread.class);

    private Integer uid;

    private UserTaskMsgClientImpl qcMsgClient;

    private UserExtService userExtService;

    public UserInformationPerfectDegreeThread(Integer uid,UserTaskMsgClientImpl qcMsgClient,UserExtService userExtService){
        this.uid = uid;
        this.qcMsgClient = qcMsgClient;
        this.userExtService = userExtService;
    }
    @Override
    public void run() {
        logger.info("开始为用户进行资料完善度计算：uid 为【{}】,时间为【{}】",uid, DateUtils.getTimeStringYYYYMMDDHHMMSS(new Date()));
        TbUserExt userExt = this.userExtService.getTbUserExt(uid);
        if (userExt != null) {
            Byte baseState = userExt.getBaseState();
            double oldPerfectDegree = userExt.getPerfectDegree();
            TbUser user = this.userExtService.getUser(uid);
                if (user != null) {
                    String perfectStatus = userExt.getPerfectStatus();
                    UserInfoPerfectStatus userInfoPerfectStatus;
                    if (StringUtil.isBlankOrNull(perfectStatus)) {
                        userInfoPerfectStatus = new UserInfoPerfectStatus();
                    }else {
                        String str = userExt.getPerfectStatus();
                        userInfoPerfectStatus = GsonUtils.jsonStrToEntity(str,UserInfoPerfectStatus.class);
                    }
                        int isDefaultIcon = userInfoPerfectStatus.getIsDefaultIcon();
                        int isNameVerify = userInfoPerfectStatus.getIsNameVerify();
                        int isDefaultNickname = userInfoPerfectStatus.getIsDefaultNickname();
                        int isDefaultGender = userInfoPerfectStatus.getIsDefaultGender();
                        int isDefaultBirthday = userInfoPerfectStatus.getIsDefaultBirthday();
                        int isDefaultIndustry = userInfoPerfectStatus.getIsDefaultIndustry();
                        int isDefaultSign = userInfoPerfectStatus.getIsDefaultSign();
                        int isDefaultHeight = userInfoPerfectStatus.getIsDefaultHeight();
                        int isDefaultWeight = userInfoPerfectStatus.getIsDefaultWeight();

                        int total = isDefaultIcon + isNameVerify + isDefaultNickname + isDefaultGender + isDefaultBirthday + isDefaultIndustry + isDefaultSign + isDefaultHeight + isDefaultWeight;
                        if (baseState.equals(UserConstants.USER_BASE_STATE_FALSE)) {
                            int baseSum = isDefaultIcon + isDefaultNickname + isDefaultBirthday + isDefaultGender;
                            if(baseSum == 4){
                                userExt.setBaseState(UserConstants.USER_BASE_STATE_TRUE);
                            }
                        }
                        int taskSum = isDefaultIcon + isDefaultNickname + isDefaultBirthday + isDefaultGender;
                        if (taskSum == 4) {
                            /**-------------------------------任务消息---------------------------**/
                            UserTaskMsg userTaskMsg = new UserTaskMsg();
                            userTaskMsg.setTaskId(2);
                            userTaskMsg.setUid(uid);
                            logger.info("完善资料 开始发送消息到mq:{}, 发送人：【{}】",userTaskMsg.toString(),uid);
                            MqRequestMsg mqRequestMsg = new MqRequestMsg();
                            mqRequestMsg.setTopic(Constants.MQ_TOPIC_TASK_TOPIC);
                            mqRequestMsg.setTag(Constants.MQ_TOPIC_TASK_TAG);
                            mqRequestMsg.setBody(ObjectUtil.toByteArray(userTaskMsg));
                            qcMsgClient.asyncSend(mqRequestMsg);
                            logger.info("完善资料 发送消息完毕==========》");
                            /**-------------------------------任务消息---------------------------**/
                        }
                        double perfectDegree = total/10.0;
                        userExt.setPerfectDegree(perfectDegree);
                        String json = GsonUtils.toJson(userInfoPerfectStatus);
                        userExt.setPerfectStatus(json);
                        userExtService.updateUserExt(userExt);
                    /**-------------------------------俱乐部完美无缺勋章---------------------------**/
                        if (oldPerfectDegree < 0.9 && perfectDegree >= 0.9) {
                            ClubMedalMsg clubMedalMsg = new ClubMedalMsg();
                            clubMedalMsg.setMedalId(SysClubMedalConditionEnum.PERFECT.getId());
                            clubMedalMsg.setUid(uid);
                            logger.info("资料完善度达到90% 开始发送消息到mq:{}, 发送人：【{}】",clubMedalMsg.toString(),uid);
                            MqRequestMsg mqRequestMsg = new MqRequestMsg();
                            mqRequestMsg.setMsgId(UUIDUtils.getUUID());
                            mqRequestMsg.setTopic(MqConstants.ClubMedalConsumer.CLUB_MEDAL_TOPIC);
                            mqRequestMsg.setTag(MqConstants.ClubMedalConsumer.CLUB_MEDAL_TAGS);
                            mqRequestMsg.setBody(ObjectUtil.toByteArray(clubMedalMsg));
                            qcMsgClient.asyncSend(mqRequestMsg);
                            logger.info("资料完善度达到90% 消息发送完毕");
                    }
                    /**-------------------------------俱乐部完美无缺勋章---------------------------**/

                }else{
                    logger.error("该用户信息不存在 uid 为：【{}】,时间为【{}】", uid, DateUtils.getTimeStringYYYYMMDDHHMMSS(new Date()));
                }
        }else{
            logger.error("该用户拓展信息不存在 uid 为：【{}】,时间为【{}】",uid, DateUtils.getTimeStringYYYYMMDDHHMMSS(new Date()));
        }

    }




}
